<?php

session_start();

require_once("includes/connect.php");
require_once("includes/function.php");

//$pet_id = $_REQUEST["pet_id"];

$id = $_REQUEST["id"];


$db = new myDBS;
$Cur = new Currency;

$sql = "SELECT DISTINCT tbl_pet.*, expenses1_name, expenses2_name, panngan2_name, panngan3_name"
        . " , panngan2.panngan2_id, panngan3.panngan3_id, expenses_lvl2.e_id, finance_id, verify_date, comment "
        . " FROM tbl_pet, expenses_lvl1, expenses_lvl2, panngan2, panngan3  "
        . "WHERE id  = '$id' "
        . " and expenses_lvl1.expenses1_id = ex1_id "
        . " and expenses_lvl2.expenses2_id = ex2_id "
        . " and panngan2.panngan2_id = tbl_pet.panngan2_id "
        . " and panngan3.panngan3_id = tbl_pet.panngan3_id";
//echo $sql;
$result = $db->runSQL($sql);
$rows = $db->fetch_object($result);

$elass = gen_account_id($rows->panngan2_id, $rows->panngan3_id, $rows->e_id);

//$budget = get_budget($rows->panngan2_id, $rows->panngan3_id, $rows->ex1_id, $rows->ex2_id, "", $rows->budget_year);
//
//$budget_use = get_budget_used($rows->panngan2_id, $rows->panngan3_id, $rows->ex1_id, $rows->ex2_id, $rows->ex3_id, $rows->budget_year);
//
//$budget_remain = $budget - $budget_use;

// just require TCPDF instead of FPDF
//define('FPDF_FONTPATH','fpdf/font/');
require_once('fpdf/TCPDF/tcpdf.php');
require_once('fpdf/FPDI/fpdi.php');

class PDF extends FPDI {

    /**
     * "Remembers" the template id of the imported page
     */
    var $_tplIdx;

    /**
     * include a background template for every page
     */
    function Header() {
        if (is_null($this->_tplIdx)) {
            $this->numPages = $this->setSourceFile('form/form1.pdf');

            $this->_tplIdx = $this->importPage(1);
        }
        $this->useTemplate($this->_tplIdx);

        /*
          //$this->AddFont('THSarabunNew','','THSarabunNew.php');
          //$this->SetFont('THSarabunNew', '', 9);
          $this->SetTextColor(255);
          $this->SetXY(60.5, 24.8);
          $this->Cell(0, 8.6, "TCPDF and FPDI");
         */
    }

    function Footer() {
        
    }

}

// initiate PDF
$pdf = new PDF($orientation = 'P', $unit = 'mm', "A4", $unicode = true, $encoding = 'UTF-8', $diskcache = false, $pdfa = false);
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
//$pdf->SetAutoPageBreak(true, 40);
$pdf->setFontSubsetting(false);

// add a page
$pdf->AddPage();

$vertical_align = 1;

//$pdf->AddFont('THSarabunNew','','THSarabunNew.php');
//$pdf->SetFont("freeserif", "", 12);
$pdf->SetFont('thsarabun', '', 14);
// now write some text above the imported page
$pdf->SetXY(30, 27 + $vertical_align);
//$pdf->Write(0, $_SESSION["lvl1"]);
$pdf->Write(0, "สำนักงานเทศบาลนครอุดรธานี");

$pdf->SetXY(32, 34 + $vertical_align);
$pdf->Write(0, thainumDigit($rows->budget_year));

$pdf->SetXY(92, 34 + $vertical_align);
$pdf->Write(0, thainumDigit($rows->pet_id));

$pdf->SetXY(155, 34 + $vertical_align);
$pdf->Write(0, thainumDigit($rows->finance_id));

$pdf->SetXY(24, 40.5 + $vertical_align);
$pdf->Write(0, $rows->panngan2_name);

$pdf->SetXY(155, 47 + $vertical_align);
$pdf->Write(0, thainumDigit($elass));

$pdf->SetXY(84, 40.5 + $vertical_align);
$pdf->Write(0, $rows->panngan3_name);

//finance_id
$pdf->SetXY(157, 40.5 + $vertical_align);
if ($rows->verify_date != "0000-00-00 00:00:00") 
    $pdf->Write(0, thainumDigit(date_format(date_create($rows->verify_date), "Y-m-d")));


$pdf->SetXY(30, 47 + $vertical_align);
$pdf->MultiCell(42, 28, $rows->expenses1_name, 0, '', 0, 0, '', '', true);

$pdf->SetXY(87, 47 + $vertical_align);
$pdf->MultiCell(49, 13, $rows->expenses2_name, 0, '', 0, 0, '', '', true);
//$pdf->Write(0, $rows->expenses2_name);

$pdf->SetXY(86, 60 + $vertical_align);
$pdf->MultiCell(114, 14, $rows->expenses3_name, 0, '', 0, 0, '', '', true);
//$pdf->Write(0, $rows->expenses3_name);

$pdf->SetXY(76, 80 + $vertical_align);
$pdf->Cell(41, 0, thainumDigit(number_format($rows->amount, 2)), 0, 0, 'R');
$pdf->SetXY(76, 87 + $vertical_align);
$pdf->Cell(41, 0, thainumDigit(number_format($rows->tt, 2)), 0, 0, 'R');
$pdf->SetXY(76, 94 + $vertical_align);
$pdf->Cell(41, 0, thainumDigit(number_format($rows->amount1, 2)), 0, 0, 'R');
$pdf->SetXY(76, 101 + $vertical_align);
$pdf->Cell(41, 0, thainumDigit(number_format($rows->tt1, 2)), 0, 0, 'R');
$pdf->SetXY(76, 108 + $vertical_align);
$pdf->Cell(41, 0, thainumDigit(number_format($rows->penalty, 2)), 0, 0, 'R');

$pdf->SetXY(117, 80 + $vertical_align);
if ($rows->comment) {
    $pdf->MultiCell(83, 47, $rows->comment, 0, '', 0, 1, '', '', true);
} else {
    $pdf->MultiCell(83, 47, $_SESSION["lvl2"] . " ประจำ " . $rows->month_year, 0, '', 0, 1, '', '', true);
}


$pdf->SetXY(76, 120 + $vertical_align);
$pdf->Cell(41, 0, thainumDigit(number_format($rows->net, 2)), 0, 0, 'R');

$pdf->SetXY(56, 126.5 + $vertical_align);
$pdf->Write(0, $Cur->bahtThai($rows->net));

$pdf->SetXY(85, 132 + $vertical_align);
$pdf->Cell(41, 0, thainumDigit(number_format($rows->budget_remain, 2)), 0, 0, 'R');

$pdf->SetXY(94, 139 + $vertical_align);
$pdf->Cell(41, 0, thainumDigit(number_format($rows->budget_remain_this, 2)), 0, 0, 'R');

$pdf->SetXY(22, 179 + $vertical_align);
$pdf->Cell(39, 0, thainumDigit(number_format($rows->net, 2)), 0, 0, 'C');

$pdf->SetXY(90, 179 + $vertical_align);
$pdf->Cell(39, 0, thainumDigit(number_format($rows->net, 2)), 0, 0, 'C');

$pdf->SetXY(157, 179 + $vertical_align);
$pdf->Cell(39, 0, thainumDigit(number_format($rows->net, 2)), 0, 0, 'C');

$pdf->SetXY(29, 232 + $vertical_align);
$pdf->Cell(36, 0, thainumDigit(number_format($rows->net, 2)), 0, 0, 'C');

$pdf->SetXY(64, 232 + $vertical_align);
$pdf->Write(0, $Cur->bahtThai($rows->net));


$pdf->Output('output_petition1.pdf', 'I');
?>